(function (win, angular) {
    'use strict';
    angular.module('app')
        .factory('eChartLineFactory', [
            function () {
                var factory;
                factory= {
                    initData:function(data,model){  //修改数据结构//model 为 net，mem 用于单位转换  
                        var len=data.length,i=0,obj,chartDatas=[];
                        for(i=0;i<len;i++){
                            obj={};
                            obj.name=data[i].ns;
                            obj.value=[];
                            obj.value.push(data[i].clock*1000);
                            if(model==='net'){
                            	obj.value.push(data[i].value/1024);
                            }else if(model==='mem'){
                            	obj.value.push(data[i].value/(1024*1024));
                            }else{
                            	obj.value.push(data[i].value);
                            }
                            
                            chartDatas.push(obj);
                        }
                        return chartDatas;
                    },
                    setChartConfig: function(conf){  //设置配置项
                        var option,obj,i=1;
                        option={
                            title:{
                                text:conf.text
                            },
                            xAxis: {
                                type: 'time',
                                splitLine: {
                                    show: false
                                }
                            },
                            yAxis: {
                                type: 'value',
                                boundaryGap: [0, '100%'],
                                splitLine: {
                                    show: false
                                }
                            },
                            series:[
                                {
                                    name: conf.seriesNames[0],
                                    type: 'line',
                                    showSymbol: false,
                                    hoverAnimation: false,
                                    data: conf.data[0]
                                }
                            ] 
                        };
                        if(conf.data.length>1){  //有多条线时
                        	option.legend={
                    			data:conf.seriesNames
                        	};
                        	for(var i=1;i<conf.data.length;i++){
                        		obj={ 
                    				name: conf.seriesNames[i],
                                    type: 'line',
                                    showSymbol: false,
                                    hoverAnimation: false,
                                    data: conf.data[i]
                        		};
                        		option.series.push(obj);
                        	}
                        }
                        return option;
                    },
                    changData:function(conf){  //{old:,data:[],type:,model:} //model 为 net，mem 用于单位转换  
                        var len=conf.data.length,i=0,obj,returnArr=[],s;
                        //console.log(angular.copy(conf.old));
                        if(conf.type==='static'){
                        	if(conf.data.length>0){
                        		for(i=0;i<conf.data.length;i++){
                        			returnArr.push({data:this.initData(conf.data[i],conf.model)})
                        		}
                        	}
                            return returnArr;
                        }else{
                            
                            for(i=0;i<len;i++){
                            	
                            	conf.old[i].data.shift();
                            	for(s=0;s<conf.data[i].length;s++){
                            		obj={};
                                    obj.name=conf.data[i][s].ns;
                                    obj.value=[];
                                    obj.value.push(new Date());
                                    if(conf.model === 'net'){
                                    	obj.value.push(conf.data[i][s].value/1024);
                                    }else if(conf.model==='mem'){
                                    	obj.value.push(conf.data[i][s].value/(1024*1024));
                                    }else{
                                    	obj.value.push(conf.data[i][s].value);
                                    }
                                    conf.old[i].data.push(obj);
                            	}
                                
                            }
                            //console.log(conf.old);
                            return conf.old;
                        }
                    }
                };
                return factory;
            }]);
})(window, window.angular);
